package com.csproj.basic.resource;

import com.codahale.metrics.annotation.Timed;
import com.csproj.basic.domain.Path;
import com.csproj.basic.service.PathService;
import com.csproj.basic.service.dto.AreaDTO;
import com.csproj.basic.service.dto.PathDTO;
import com.csproj.siiat.base.common.dto.BaseDTO;
import com.csproj.siiat.base.common.repository.PageableTools;
import com.csproj.system.domain.UserInfo;
import com.csproj.system.service.SysLogsService;
import com.csproj.system.service.UserInfoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.net.URISyntaxException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * REST controller for managing Path.
 */
@RestController
@RequestMapping("/api/")
public class PathResource {

    private final Logger log = LoggerFactory.getLogger(PathResource.class);

    private static final String ENTITY_NAME = "path";

    private final PathService pathService;
    private final UserInfoService userInfoService;
    private final SysLogsService sysLogsService;
    public PathResource(PathService pathService,UserInfoService userInfoService,SysLogsService sysLogsService) {
        this.pathService = pathService;
        this.userInfoService =userInfoService;
        this.sysLogsService =sysLogsService;
    }

    /**
     * POST  /path : 创建路径
     *
     * @param pathDTO 路径DTO
     * @return TODO 保存post方法
     * TODO 每个方面里面都要加try catch处理
     */
    @PostMapping("/path")
    @Timed
    public BaseDTO createPath(@Valid @RequestBody PathDTO pathDTO) {
        pathDTO.setEnableFlag(true);
        PathDTO result = pathService.saveAndGetDTO(pathDTO);
        UserInfo userInfo = userInfoService.getCurrentUser();
        sysLogsService.saveForGet("", userInfo.getName()+":user_create_path","");
        return BaseDTO.createBaseDTO().setData(result);
    }


    /**
     * PUT  /path : 修改路径.
     *
     * @param pathDTO 路径.DTO
     * @return
     */
    @PutMapping("/path/{id}")
    @Timed
    public BaseDTO updatePath(@Valid @RequestBody PathDTO pathDTO, @PathVariable String id) throws URISyntaxException {
        pathDTO.setId(id);
        PathDTO result = pathService.updateAndGetDTO(pathDTO);

        UserInfo userInfo = userInfoService.getCurrentUser();
        sysLogsService.saveForGet("", userInfo.getName()+":user_update_path","");
        return BaseDTO.createBaseDTO().setData(result);
    }

    /**
     * GET  /areas : 路径装箱.
     *
     * @return 路径信息
     */
    @GetMapping("/path/all")
    @Timed
    public BaseDTO getAllPath() {
        List<PathDTO> lists = pathService.findAllAndGetDTO();
        return BaseDTO.createBaseDTO().setFieldList(lists);
    }

    /**
     * GET  /path : 获取路径信息.
     *
     * @return 路径信息
     */
    @GetMapping("/path")
    @Timed
    public BaseDTO getPagePaths(@RequestParam(value = "pageNumber", required = false) Integer pageNum,
                                @RequestParam(value = "pageSize", required = false) Integer pageSize,
                                @RequestParam(value = "name", required = false) String name,
                                @RequestParam(value = "companyId", required = false) String companyId,
                                @RequestParam(value = "startSiteId", required = false) String startSiteId,
                                @RequestParam(value = "endSiteId", required = false) String endSiteId

    ) {
        Pageable pageable = PageableTools.basicPage(pageNum, pageSize);
        Map<String, Object> map = new HashMap<>();
        map.put("pathNo", name);
        map.put("startSiteId",startSiteId);
        map.put("endSiteId", endSiteId);
        BaseDTO result = pathService.findPageListAndGetDTO(map, pageable);

        UserInfo userInfo = userInfoService.getCurrentUser();
        sysLogsService.saveForGet("", userInfo.getName()+":user_view_path_list","");
        return result;
    }

    /**
     * GET  /path : 获取路径信息.
     *
     * @return 路径信息
     */
    @GetMapping("/path/site/{siteId}")
    @Timed
    public BaseDTO getPathListBySiteId(@PathVariable(value = "siteId", required = false) String siteId) {
        Map<String, Object> map = new HashMap<>();
        map.put("startSiteId", siteId);
        List<Path> list = pathService.findListBySiteId(map);
        return BaseDTO.createBaseDTO().setFieldList(list);
    }

    /**
     * GET  /path/:id : 根据路径id，获取路径信息
     *
     * @param id 路径id
     * @return 路径信息
     */
    @GetMapping("/path/{id}")
    @Timed
    public BaseDTO getPath(@PathVariable String id) {
        PathDTO areaDTO = pathService.findOneAndGetDTO(id);
        return BaseDTO.createBaseDTO().setData(areaDTO);
    }

    /**
     * DELETE  /path/:id : 根据id，删除路径.
     *
     * @param id id
     * @return
     */
    @DeleteMapping("/path/{id}")
    @Timed
    public BaseDTO deletePath(@PathVariable String id) {
        pathService.delete(id);
        return BaseDTO.createBaseDTO().setMessage("delete_success");
    }

    /**
     * 根据路径ids，批量删除路径
     *
     * @param ids ids
     * @return
     */
    @DeleteMapping("/path/deletePaths")
    @Timed
    public BaseDTO deleteIds(@RequestBody(required = false) String[] ids) {
        pathService.deleteIds(ids);
        return BaseDTO.createBaseDTO().setMessage("batch_delete_success");
    }

    /**
     * 上传文件
     *
     * @param file file
     * @return
     */
    @PostMapping("/path/uploadFile")
    @Timed
    public BaseDTO uploadFile(HttpServletRequest request, @RequestParam("file") MultipartFile file) {
        String filePath = pathService.uploadFile(request, file);
        Map<String, Object> map = new HashMap<>();
        map.put("url", filePath);
        return BaseDTO.createBaseDTO(map).setMessage("upload_file_success");
    }

    @PutMapping("/path/enableFlag")
    @Timed
    public BaseDTO updateStatus(@RequestBody PathDTO pathDTO) {

        pathDTO = pathService.updateEnableFlag(pathDTO);
        UserInfo userInfo = userInfoService.getCurrentUser();
        sysLogsService.saveForGet("", userInfo.getName()+":user_update_path_status","");

        return BaseDTO.createBaseDTO(pathDTO);
    }
}